package dbutil

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type IDModel struct {
	ID uint64 `json:"id" gorm:"type:BIGINT;NOT NULL;primarykey"` //
	TimeModel
}

type TimeModel struct {
	CreatedAt time.Time `gorm:"type:datetime"`
	UpdatedAt time.Time `gorm:"type:datetime"`
}

func (m *TimeModel) BeforeCreate(db *gorm.DB) error {
	m.CreatedAt = time.Now()
	m.UpdatedAt = time.Now()
	return nil
}

type UIDModel struct {
	ID string `gorm:"type:varchar(64);NOT NULL;primarykey"` //
	TimeModel
}

func (m *UIDModel) BeforeCreate(db *gorm.DB) error {
	if m.ID == "" {
		m.ID = uuid.New().String()
	}
	return m.TimeModel.BeforeCreate(db)
}
